package com.framework.web.controller.common;

import com.framework.common.annotation.QueryTarget;
import com.framework.common.model.response.R;
import com.framework.common.model.validation.ValidationGroup;
import com.framework.common.util.other.NumeralUtil;
import com.framework.common.util.system.SystemUtil;
import com.framework.model.common.vo.TypeVo;
import com.framework.service.common.TypeService;
import com.framework.web.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.web.controller.system
 * @description 类型请求控制类
 * @datetime 2019-01-09 11:11:11
 */
@Validated
@Controller
@RequestMapping(value = "/common/type/item")
public class TypeItemController extends BaseController {
    private String path = "lowerright/common/type/item/";
    @Autowired
    private TypeService typeServiceImpl;


    /**
     * @return org.springframework.web.servlet.ModelAndView
     * @title 分页页面跳转
     * @description 分页页面跳转
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @GetMapping("/page/list")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:type_item_view')")
    public ModelAndView pageList(@NotNull(message = "请选择上级")
                                 @Min(value = NumeralUtil.POSITIVE_ONE, message = "上级不存在")
                                 @Max(value = Long.MAX_VALUE, message = "上级不存在")
                                         Long parentId) {
        ModelAndView mv = new ModelAndView(path + "typeItemList");
        mv.addObject("parent", typeServiceImpl.selectByPrimaryKey(parentId));
        return mv;
    }

    /**
     * @return org.springframework.web.servlet.ModelAndView
     * @title 新增页面跳转
     * @description 新增页面跳转
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @GetMapping("/get/add")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:itemAdd')")
    public ModelAndView getAdd(@NotBlank(message = "请选择上级")
                               @Size(message = "上级错误", max = 128)
                               @RequestParam(value = "parentTypeCode")
                                       String parentTypeCode) {
        ModelAndView mv = new ModelAndView(path + "typeItemAdd");
        mv.addObject("parent", typeServiceImpl.getByParentTypeCodeInfo(parentTypeCode));
        return mv;
    }

    /**
     * @param id 1 编号
     * @return org.springframework.web.servlet.ModelAndView
     * @title 编辑页面跳转
     * @description 编辑页面跳转
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @GetMapping("/get/edit")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:itemEdit')")
    public ModelAndView getEdit(@NotNull(message = "请选择类型")
                                @Min(value = NumeralUtil.POSITIVE_ONE, message = "类型不存在")
                                @Max(value = Long.MAX_VALUE, message = "类型不存在")
                                        Long id,
                                @NotBlank(message = "请选择上级")
                                @Size(message = "上级错误", max = 128)
                                @RequestParam(value = "parentTypeCode")
                                        String parentTypeCode) {
        ModelAndView mv = new ModelAndView(path + "typeItemEdit");
        mv.addObject("parent", typeServiceImpl.getByParentTypeCodeInfo(parentTypeCode));
        mv.addObject("p", typeServiceImpl.getByIdParam(id, parentTypeCode));
        return mv;
    }

    /**
     * @param id 1 编号
     * @return org.springframework.web.servlet.ModelAndView
     * @title 查看页面跳转
     * @description 查看页面跳转
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @GetMapping("/get/view")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:itemView')")
    public ModelAndView getView(@NotNull(message = "请选择类型")
                                @Min(value = NumeralUtil.POSITIVE_ONE, message = "类型不存在")
                                @Max(value = Long.MAX_VALUE, message = "类型不存在")
                                        Long id,
                                @NotBlank(message = "请选择上级")
                                @Size(message = "上级错误", max = 128)
                                @RequestParam(value = "parentTypeCode")
                                        String parentTypeCode) {
        ModelAndView mv = new ModelAndView(path + "typeItemView");
        mv.addObject("parent", typeServiceImpl.getByParentTypeCodeInfo(parentTypeCode));
        mv.addObject("p", typeServiceImpl.getByIdParam(id, parentTypeCode));
        return mv;
    }

    /**
     * @param row 1 公共类型类
     * @return com.framework.common.model.response.R
     * @title 分页查询公共类型列表
     * @description 分页查询公共类型列表
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    // method = RequestMethod.POST,
    @GetMapping(value = "/find/page/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @QueryTarget
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:item_find_page_list')")
    public R<?> findPageList(@Validated(value = {ValidationGroup.formPageQuery.class, ValidationGroup.formQueryItem.class}) TypeVo row) {
        return typeServiceImpl.findParamPageList(row, NumeralUtil.POSITIVE_ONE);
    }

    /**
     * @param row 1 公共类型类
     * @return com.framework.common.model.response.R
     * @title 新增公共类型
     * @description 新增公共类型
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:itemAdd')")
    public R<?> save(@Validated(value = {ValidationGroup.formAddItem.class}) TypeVo row) {
        return typeServiceImpl.save(row);
    }

    /**
     * @param row 1 公共类型类
     * @return com.framework.common.model.response.R
     * @title 编辑公共类型
     * @description 编辑公共类型
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:itemEdit')")
    public R<?> edit(@Validated(value = {ValidationGroup.formEditItem.class}) TypeVo row) {
        return typeServiceImpl.edit(row);
    }

//    /**
//     * @param idList 1 类型编号集合
//     * @return com.framework.common.model.response.R
//     * @title 批量删除
//     * @description 批量删除
//     * @author 龘鵺
//     * @datetime 2019-01-09 11:11:11
//     */
//    @PostMapping(value = "/batch/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ResponseBody
//    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:itemBatchDel')")
//    public R<?> batchDel(@NotEmpty(message = "请选择类型")
//                                   @Size(min = NumeralUtil.POSITIVE_ONE, message = "类型不存在")
//                                   @RequestParam(value = "idList[]")
//                                           List<Long> idList,
//                                   @NotBlank(message = "请选择上级")
//                                   @Size(message = "上级错误", max = 128)
//                                           String parentTypeCode) {
//        return typeServiceImpl.batchDeleteList(idList, parentTypeCode);
//    }

    /**
     * @param id 1 类型编号
     * @return com.framework.common.model.response.R
     * @title 删除
     * @description 删除
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @DeleteMapping(value = "/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:itemDel')")
    public R<?> del(@NotNull(message = "请选择类型")
                              @Min(value = NumeralUtil.POSITIVE_ONE, message = "类型不存在")
                              @Max(value = Long.MAX_VALUE, message = "类型不存在")
                              @RequestParam(value = "id") Long id,
                              @NotBlank(message = "请选择上级")
                              @Size(message = "上级错误", max = 128)
                              @RequestParam(value = "parentTypeCode")
                                      String parentTypeCode) {
        return typeServiceImpl.delete(id, parentTypeCode);
    }

//    /**
//     * @param id       1 类型编号
//     * @param typeCode 2 类型代码
//     * @return com.framework.common.model.response.R
//     * @title 验证是否重复类型code
//     * @description 验证是否重复类型code
//     * @author 龘鵺
//     * @datetime 2019-01-09 11:11:11
//     */
//    @GetMapping("/isExist")
//    @ResponseBody
//    public R<?> isExist(Long id, String typeCode) {
//        return typeServiceImpl.isExist(id, typeCode);
//    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 启用禁用
     * @description 启用禁用
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     */
    @GetMapping(value = "/enable", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','TYPE_LIST_MANAGEMENT:itemEnable')")
    public R<?> enable(@NotNull(message = "请选择类型")
                                 @Min(value = NumeralUtil.POSITIVE_ONE, message = "类型不存在")
                                 @Max(value = Long.MAX_VALUE, message = "类型不存在")
                                 @RequestParam(value = "id") Long id,
                                 @NotBlank(message = "请选择上级")
                                 @Size(message = "上级错误", max = 128)
                                 @RequestParam(value = "parentTypeCode")
                                         String parentTypeCode) {
        return typeServiceImpl.enable(id, parentTypeCode);
    }
}
